package com.wdcg.model.access;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;

import android.util.Log;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.wdcg.model.dao.Bonus;
import com.wdcg.model.dao.BonusDescription;
import com.wdcg.model.dao.BonusType;
import com.wdcg.model.dao.BonusTypeDescription;
import com.wdcg.model.dao.Dwarf;
import com.wdcg.model.dao.DwarfDescription;
import com.wdcg.model.dao.PlayerLocale;
import com.wdcg.model.dbhelper.DatabaseHelper;

public class BonusAccess {
	public static ArrayList<BonusDescription> getBonusesDescriptions(){
		DatabaseHelper dbh=DatabaseHelper.getMainDBHelper();
		
		ArrayList<BonusDescription> result;
		QueryBuilder<BonusDescription, Integer> bonusDescQB = dbh.getBonusDescriptionDao().queryBuilder();
				
		try {
			PlayerLocale userLang = LocaleAccess.getCurrUserLocale();
			bonusDescQB.where().eq(PlayerLocale.LOCALE_ID_COLUMN,
					userLang.getId());

			result = (ArrayList<BonusDescription>) bonusDescQB.query();
		} catch (Exception e1) {
			result = new ArrayList<BonusDescription>();
			e1.printStackTrace();
		}
		
		Log.i("getBonusesDescriptions()",
				"method executed successfully, result=" + result.size());
		return result;
	}
	
	public static ArrayList<BonusDescription> getActiveBonusesDescription(){
			DatabaseHelper dbh=DatabaseHelper.getMainDBHelper();
			
			ArrayList<BonusDescription> result;
			QueryBuilder<Bonus, Integer> bonusQB = dbh.getBonusDao().queryBuilder();
			QueryBuilder<BonusDescription, Integer> bonusDescQB = dbh.getBonusDescriptionDao().queryBuilder();
			QueryBuilder<BonusType, Integer> bonusTypeQB = dbh.getBonusTypeBao().queryBuilder();
			//QueryBuilder<BonusTypeDescription, Integer> bonusTypeDescQB = dbh.getBonusTypeDescriptionDao().queryBuilder();

			try {
				PlayerLocale userLang = LocaleAccess.getCurrUserLocale();
				bonusDescQB.where().eq(PlayerLocale.LOCALE_ID_COLUMN,
						userLang.getId());
				bonusQB.join(bonusTypeQB);
				bonusDescQB.join(bonusQB);
				
				result = (ArrayList<BonusDescription>) bonusDescQB.query();
			} catch (Exception e1) {
				result = new ArrayList<BonusDescription>();
				e1.printStackTrace();
			}
			
			Log.i("getVisibleDwarvesDescriptions()",
					"method executed successfully, result=" + result.size());
			return result;
	}
	
	public static BonusTypeDescription getBonusTypeDescriptionForBonusDescription(BonusDescription bonusDescription){
		DatabaseHelper dbh=DatabaseHelper.getMainDBHelper();
		
		ArrayList<BonusTypeDescription> result;
	
		QueryBuilder<BonusTypeDescription, Integer> bonusTypeDescQB = dbh.getBonusTypeDescriptionDao().queryBuilder();
		QueryBuilder<BonusType, Integer> bonusTypeQB = dbh.getBonusTypeBao().queryBuilder();

		try {
			PlayerLocale userLang = LocaleAccess.getCurrUserLocale();

			bonusTypeDescQB.where().eq(PlayerLocale.LOCALE_ID_COLUMN, userLang.getId());
			bonusTypeQB.where().eq(BonusType.BONUS_TYPE_ID_COLUMN, bonusDescription.getBonus().getBonusType().getId());
			bonusTypeDescQB.join(bonusTypeQB);
			
			result = (ArrayList<BonusTypeDescription>) bonusTypeDescQB.query();
		} catch (Exception e1) {
			result = null;
			e1.printStackTrace();
		}
		
		Log.i("getVisibleDwarvesDescriptions()",
				"method executed successfully, result=" + result.size());
		return result.get(0);
	}
	
	public static void setRandomBonusTypeForBonusDescription(BonusDescription bonusDescription){
		DatabaseHelper dbh=DatabaseHelper.getMainDBHelper();
		QueryBuilder<BonusType, Integer> bonusTypeQB = dbh.getBonusTypeBao().queryBuilder();

		try {
			
			ArrayList<BonusType> bonusTypes = (ArrayList<BonusType>) bonusTypeQB.query();
			BonusType bonusType= bonusTypes.get(new Random().nextInt(bonusTypes.size()));
			bonusDescription.getBonus().setBonusType(bonusType);
			dbh.getBonusDao().update(bonusDescription.getBonus());

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void clearBonusTypeForBonusDescription(BonusDescription bonusDescription){
		DatabaseHelper dbh=DatabaseHelper.getMainDBHelper();

		try {
			bonusDescription.getBonus().setBonusType(null);
			dbh.getBonusDao().update(bonusDescription.getBonus());

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static  BonusDescription getBonusDescriptionById(Integer bonusDescId){
		DatabaseHelper dbh=DatabaseHelper.getMainDBHelper();
		
		ArrayList<BonusDescription> result;
		QueryBuilder<BonusDescription, Integer> bonusDescQB = dbh.getBonusDescriptionDao().queryBuilder();

		try {
			
			bonusDescQB.where().eq(BonusDescription.BONUS_DESCRIPTION_ID_COLUMN, bonusDescId);
		
			result = (ArrayList<BonusDescription>) bonusDescQB.query();
		} catch (Exception e1) {
			result = new ArrayList<BonusDescription>();
			e1.printStackTrace();
		}
		
		Log.i("getVisibleDwarvesDescriptions()",
				"method executed successfully, result=" + result.size());
		return result.get(0);
}
	
	public static  BonusTypeDescription getBonusTypeDescriptionById(Integer bonusTypeDescId){
		DatabaseHelper dbh=DatabaseHelper.getMainDBHelper();
		
		ArrayList<BonusTypeDescription> result;
		QueryBuilder<BonusTypeDescription, Integer> bonusTypeDescQB = dbh.getBonusTypeDescriptionDao().queryBuilder();

		try {
			
			bonusTypeDescQB.where().eq(BonusDescription.BONUS_DESCRIPTION_ID_COLUMN, bonusTypeDescId);
		
			result = (ArrayList<BonusTypeDescription>) bonusTypeDescQB.query();
		} catch (Exception e1) {
			result = new ArrayList<BonusTypeDescription>();
			e1.printStackTrace();
		}
		
		return result.get(0);
}

	public static ArrayList<BonusDescription> getAvailableBonusesDesctiption(){
		ArrayList<BonusDescription> result;
		DatabaseHelper dbh=DatabaseHelper.getMainDBHelper();

		QueryBuilder<Bonus, Integer> bonusQB = dbh.getBonusDao().queryBuilder();
		QueryBuilder<BonusDescription, Integer> bonusDescQB = dbh.getBonusDescriptionDao().queryBuilder();
		QueryBuilder<BonusType, Integer> bonusTypeQB = dbh.getBonusTypeBao().queryBuilder();

		try {
			PlayerLocale userLang = LocaleAccess.getCurrUserLocale();
			
			bonusDescQB.where().eq(PlayerLocale.LOCALE_ID_COLUMN,
					userLang.getId());
			bonusQB.leftJoin(bonusTypeQB);
			bonusQB.where().isNull(BonusType.BONUS_TYPE_ID_COLUMN).
		    or().eq(BonusType.BONUS_TYPE_ID_COLUMN, 0).prepare();
			bonusDescQB.join(bonusQB);
			
			result = (ArrayList<BonusDescription>) bonusDescQB.query();
		} catch (Exception e1) {
			result = new ArrayList<BonusDescription>();
			e1.printStackTrace();
		}
		Log.i("Bonuses","count: "+result.size());
		for(BonusDescription bonus : result){
			Log.i("available bonus", bonus.getName());
			//Log.i("its type ","type "+ bonus.getBonus().getBonusType().getBonusType());
		}
		return result;
	}
}

